package download;

import java.io.IOException;
import java.nio.channels.SelectableChannel;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Info_report{
	String report_title;
	String report_place;
	String report_sponsor;
	String report_person;
	String report_description;
	String report_duration;
	String report_starttime;
	//Set <String> report_subject;
	String report_compere;
	String report_langurage;
	String report_ticket;
	String report_link;
	String report_listener;
	int report_state;//-1 结束， 1即将
	String report_person_introduction;
	int report_iscanceled;
	
	public void init() {
		report_title="";
		report_place="";
		report_sponsor="";
		report_person="";
		report_description="";
		report_duration="";
		report_starttime="";
		//Set <String> report_subject;
		report_compere="";
		report_langurage="";
		report_ticket="";
		report_link="";
		report_listener="";
		report_state=1;//-1 结束， 1即将
		report_person_introduction="";
	}
}


public class ReportInfo {

	Map<String,	Info_report> infoMap = new HashMap<String, Info_report>();
	
	public void getReportInfo_kyybgxx() throws IOException {
		String html = CommonUtil.getHTML("http://kyybgxx.cic.tsinghua.edu.cn/kybg/xsgg/", "GBK");
		String htmlString = new String(CommonUtil.getUTF8BytesFromGBKString(html), "UTF-8"); 
		//System.out.println(htmlString);
		Pattern pattern = Pattern.compile("category=(\\d{4})\">([\\s\\S]+?)</a></div>");
		Matcher matcher = pattern.matcher(htmlString);
		while (matcher.find()) {
			System.out.println(matcher.group(1)+"\t"+matcher.group(2));
			String catagoryUrlString = "http://kyybgxx.cic.tsinghua.edu.cn/kybg/xsgg/list_xshd_cat.jsp?boardid=35&category="+matcher.group(1);
			
			String catagoryHtmlString_GBK = CommonUtil.getHTML(catagoryUrlString, "GBK");
			String catagoryHtmlString = new String(CommonUtil.getUTF8BytesFromGBKString(catagoryHtmlString_GBK));
			//System.out.println(catagoryHtmlString);
			System.out.println("#####################");
			Pattern pat_catagory = Pattern.compile("seq=(\\d+?)&boardid=35&pageno=\">([\\s\\S]+?)</a>");
			Matcher mat_catagory = pat_catagory.matcher(catagoryHtmlString);
			while (mat_catagory.find()) {
				System.out.println(mat_catagory.group(1)+"\t"+mat_catagory.group(2));
				String singleUrlString = "http://kyybgxx.cic.tsinghua.edu.cn/kybg/xsgg/detail.jsp?seq="+mat_catagory.group(1)+"&boardid=35&pageno=";
				String singlePageString_GBK = CommonUtil.getHTML(singleUrlString, "GBK");
				String singlePageString = new String(CommonUtil.getUTF8BytesFromGBKString(singlePageString_GBK));
				//System.out.println(singlePageString);
				
				Pattern pat_infoPattern = null;
				Matcher mat_infoMatcher = null;
				Info_report info_report = new Info_report();
				
				info_report.report_link = singleUrlString;
				
				pat_infoPattern = Pattern.compile("报告时间:<[\\s\\S]+?</td>[\\s\\S]+?>([\\s\\S]+?)</td>");
				mat_infoMatcher = pat_infoPattern.matcher(singlePageString);
				if (mat_infoMatcher.find()) {
					System.out.println(mat_infoMatcher.group(1));
					try {
						Date date1 = new Date();
						DateFormat df = DateFormat.getDateTimeInstance();
						
						//Date date = sdf.parse(mat_infoMatcher.group(1)+":00");
						String kkString = mat_infoMatcher.group(1)+":00";
						if (date1.after(df.parse(kkString))) {
							//System.out.println("Yes");
							info_report.init();
							break;
						}
						else {
							//System.out.println("No!");
						}
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					info_report.report_starttime = mat_infoMatcher.group(1)+":00";
				}
			
				pat_infoPattern = Pattern.compile("<tr>[\\s\\S]+?报告题目:([\\s\\S]+?)</tr>");
				mat_infoMatcher = pat_infoPattern.matcher(singlePageString);
				if (mat_infoMatcher.find()) {
					String detailString = mat_infoMatcher.group(1);
					
					pat_infoPattern = Pattern.compile("(&nbsp;)|(')|(<[\\s\\S]+?>)");
					mat_infoMatcher = pat_infoPattern.matcher(detailString);
					info_report.report_title = mat_infoMatcher.replaceAll("");
				}
				
				pat_infoPattern = Pattern.compile("<tr>[\\s\\S]+?报告人:([\\s\\S]+?)</tr>");
				mat_infoMatcher = pat_infoPattern.matcher(singlePageString);
				if (mat_infoMatcher.find()) {
					String detailString = mat_infoMatcher.group(1);
					
					pat_infoPattern = Pattern.compile("(&nbsp;)|(')|(<[\\s\\S]+?>)");
					mat_infoMatcher = pat_infoPattern.matcher(detailString);
					info_report.report_person = mat_infoMatcher.replaceAll("");
				}
				
				pat_infoPattern = Pattern.compile("<tr>[\\s\\S]+?报告地点:([\\s\\S]+?)</tr>");
				mat_infoMatcher = pat_infoPattern.matcher(singlePageString);
				if (mat_infoMatcher.find()) {
					String detailString = mat_infoMatcher.group(1);
					
					pat_infoPattern = Pattern.compile("(&nbsp;)|(')|(<[\\s\\S]+?>)");
					mat_infoMatcher = pat_infoPattern.matcher(detailString);
					info_report.report_place = mat_infoMatcher.replaceAll("");
				}
				
				pat_infoPattern = Pattern.compile("<tr>[\\s\\S]+?主办单位:([\\s\\S]+?)</tr>");
				mat_infoMatcher = pat_infoPattern.matcher(singlePageString);
				if (mat_infoMatcher.find()) {
					String detailString = mat_infoMatcher.group(1);
					
					pat_infoPattern = Pattern.compile("(&nbsp;)|(')|(<[\\s\\S]+?>)");
					mat_infoMatcher = pat_infoPattern.matcher(detailString);
					info_report.report_sponsor = mat_infoMatcher.replaceAll("");
				}
				
				pat_infoPattern = Pattern.compile("<tr>[\\s\\S]+?简介:([\\s\\S]+?)</tr>");
				mat_infoMatcher = pat_infoPattern.matcher(singlePageString);
				if (mat_infoMatcher.find()) {
					String detailString = mat_infoMatcher.group(1);
					//System.out.println(detailString);
					pat_infoPattern = Pattern.compile("<[\\s\\S]+?>");
					mat_infoMatcher = pat_infoPattern.matcher(detailString);
					detailString = mat_infoMatcher.replaceAll("");
					
					pat_infoPattern = Pattern.compile("(&nbsp;)|(')|");
					mat_infoMatcher = pat_infoPattern.matcher(detailString);
					info_report.report_description = mat_infoMatcher.replaceAll("");
					//System.out.println(info_report.report_description);
				}
				
				infoMap.put(info_report.report_link, info_report);
				
				//return;
			}
			//return;
		}
	}
	
	public void getReportInfo_sem() throws IOException {
		String htmlString = CommonUtil.getHTML("http://www.sem.tsinghua.edu.cn/portalweb/appmanager/portal/sem?_nfpb=true&_pageLabel=P5000467211269930457709", "UTF-8");
		//System.out.println(htmlString);
		Pattern pattern = Pattern.compile("学术报告[\\s\\S]+?list_xyyw_1[\\s\\S]+?<ul>([\\s\\S]+?)</ul>");
		Matcher matcher = pattern.matcher(htmlString);
		if (matcher.find()) {
			System.out.println(matcher.group(1));
			htmlString = matcher.group(1);
			Pattern pat_catagory = Pattern.compile("a href=\"([\\s\\S]+?)\"");
			Matcher mat_catagory = pat_catagory.matcher(htmlString);
			while (mat_catagory.find()) {
				System.out.println("#####################");
				System.out.println(mat_catagory.group(1));
				String singleUrlString = mat_catagory.group(1);
				
				String singlePageString = CommonUtil.getHTML(singleUrlString, "UTF-8");
				
				Pattern pat_infoPattern = null;
				Matcher mat_infoMatcher = null;
				Info_report info_report = new Info_report();
				
				info_report.report_link = singleUrlString;
				
				if (singlePageString.equals("http://www.sem.tsinghua.edu.cn/portalweb/sem?__c=4652&u=xsbgcn/60878.htm?target=no")) {
					System.out.println("QQ");
				}
				pat_infoPattern = Pattern.compile("【时间】([\\s\\S]+?)</p>");
				mat_infoMatcher = pat_infoPattern.matcher(singlePageString);
				if (mat_infoMatcher.find()) {
				//	System.out.println(mat_infoMatcher.group(1));
					
					String allTimeString = mat_infoMatcher.group(1);
					
					pat_infoPattern = Pattern.compile("(<[\\s\\S]+?>)|([\\s]+)");
					mat_infoMatcher = pat_infoPattern.matcher(allTimeString);
					allTimeString = mat_infoMatcher.replaceAll("");
					
					pat_infoPattern = Pattern.compile("\\s*(月|年)\\s*");
					mat_infoMatcher = pat_infoPattern.matcher(allTimeString);
					allTimeString = mat_infoMatcher.replaceAll("-");
					
					pat_infoPattern = Pattern.compile("\\s*：\\s*");
					mat_infoMatcher = pat_infoPattern.matcher(allTimeString);
					allTimeString = mat_infoMatcher.replaceAll(":");
					
					pat_infoPattern = Pattern.compile("(\\d{4}(-|—)\\d{1,2}(-|—)\\d{1,2})");
					mat_infoMatcher = pat_infoPattern.matcher(allTimeString);
					String year_mon_day = "";
					String beginTimeString="";
					String endTimeString="";
					if (mat_infoMatcher.find()) {
						year_mon_day = mat_infoMatcher.group(1);
						pat_infoPattern = Pattern.compile("(\\d{1,2}(:|：)\\d{1,2}(-|—)\\d{1,2}(:|：)\\d{1,2})");
						mat_infoMatcher = pat_infoPattern.matcher(allTimeString);
						if (mat_infoMatcher.find()) {
							String clock_betweenString = mat_infoMatcher.group(1);
							
							String[] between = null;
							if (clock_betweenString.contains("-")) {
						//		System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
								between = clock_betweenString.split("-");
							}
							else if (clock_betweenString.contains("—")) {
							//	System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
								between = clock_betweenString.split("—");
							}
							//between = clock_betweenString.split("-");
							if (between.length==2) {
								beginTimeString = year_mon_day+" "+between[0]+":00";
								endTimeString = year_mon_day+" "+between[1]+":00";
							}
							else {
								info_report.init();
								continue;
							}
						}
						else {
							info_report.init();
							continue;
						}
						
					}
					else {
						info_report.init();
						continue;
					}
					
					try {
						Date date1 = new Date();
						DateFormat df = DateFormat.getDateTimeInstance();
			
						if (date1.before(df.parse(beginTimeString))) {
							//System.out.println("Yes");
							info_report.init();
							break;
						}
						else {
							Date d1 = df.parse(endTimeString);
							Date d2 = df.parse(beginTimeString);
							long diff = d1.getTime() - d2.getTime();
							long minutes = diff / (1000 * 60 );
							if (minutes<0) {
								info_report.init();
								continue;
							}
							else {
								info_report.report_duration = String.format("%d mins", minutes);
								System.out.println("duration=" + info_report.report_duration);
								info_report.report_starttime = beginTimeString;
							}
						}
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				else {
					info_report.init();
					continue;
				}
			
				pat_infoPattern = Pattern.compile("(【主讲】|【主讲人】)([\\s\\S]+?)</p>");
				mat_infoMatcher = pat_infoPattern.matcher(singlePageString);
				if (mat_infoMatcher.find() && mat_infoMatcher.groupCount()==2) {
					String detailString = mat_infoMatcher.group(2);
					
					pat_infoPattern = Pattern.compile("(&nbsp;)|(')|(<[\\s\\S]+?>|(&#160;))");
					mat_infoMatcher = pat_infoPattern.matcher(detailString);
					info_report.report_person = mat_infoMatcher.replaceAll("");
					System.out.println(info_report.report_person);
				}
				
				pat_infoPattern = Pattern.compile("(【主题】|【题目】)([\\s\\S]+?)</p>");
				mat_infoMatcher = pat_infoPattern.matcher(singlePageString);
				if (mat_infoMatcher.find() && mat_infoMatcher.groupCount()==2) {
					String detailString = mat_infoMatcher.group(2);
					
					pat_infoPattern = Pattern.compile("(&nbsp;)|(')|(<[\\s\\S]+?>)|(&#160;)");
					mat_infoMatcher = pat_infoPattern.matcher(detailString);
					info_report.report_title = mat_infoMatcher.replaceAll("");
					System.out.println(info_report.report_title);
				}
				
				pat_infoPattern = Pattern.compile("【地点】([\\s\\S]+?)</p>");
				mat_infoMatcher = pat_infoPattern.matcher(singlePageString);
				if (mat_infoMatcher.find()) {
					String detailString = mat_infoMatcher.group(1);
					
					pat_infoPattern = Pattern.compile("(&nbsp;)|(')|(<[\\s\\S]+?>)|(&#160;)");
					mat_infoMatcher = pat_infoPattern.matcher(detailString);
					info_report.report_place = mat_infoMatcher.replaceAll("");
					System.out.println(info_report.report_place);
				}
				
				pat_infoPattern = Pattern.compile("(【讲座语言】|【语言】)([\\s\\S]+?)</p>");
				mat_infoMatcher = pat_infoPattern.matcher(singlePageString);
				if (mat_infoMatcher.find() && mat_infoMatcher.groupCount()==2) {
					String detailString = mat_infoMatcher.group(2);
					
					pat_infoPattern = Pattern.compile("(&nbsp;)|(')|(<[\\s\\S]+?>)|(&#160;)");
					mat_infoMatcher = pat_infoPattern.matcher(detailString);
					info_report.report_langurage = mat_infoMatcher.replaceAll("");
					System.out.println(info_report.report_langurage);
				}
				
				pat_infoPattern = Pattern.compile("(【主办方】|【主办】)([\\s\\S]+?)</p>");
				mat_infoMatcher = pat_infoPattern.matcher(singlePageString);
				if (mat_infoMatcher.find() && mat_infoMatcher.groupCount()==2) {
					String detailString = mat_infoMatcher.group(2);
					
					pat_infoPattern = Pattern.compile("(&nbsp;)|(')|(<[\\s\\S]+?>)|(&#160;)");
					mat_infoMatcher = pat_infoPattern.matcher(detailString);
					info_report.report_sponsor = mat_infoMatcher.replaceAll("");
					System.out.println(info_report.report_sponsor);
				}
				pat_infoPattern = Pattern.compile("【演讲人简历】([\\s\\S]+?)</p>");
				mat_infoMatcher = pat_infoPattern.matcher(singlePageString);
				if (mat_infoMatcher.find()) {
					String detailString = mat_infoMatcher.group(1);
					
					pat_infoPattern = Pattern.compile("(&nbsp;)|(')|(<[\\s\\S]+?>)|(&#160;)");
					mat_infoMatcher = pat_infoPattern.matcher(detailString);
					info_report.report_person_introduction = mat_infoMatcher.replaceAll("");
					System.out.println(info_report.report_person_introduction);
				}
				
				pat_infoPattern = Pattern.compile("【摘要】([\\s\\S]+?)</p>");
				mat_infoMatcher = pat_infoPattern.matcher(singlePageString);
				if (mat_infoMatcher.find()) {
					String detailString = mat_infoMatcher.group(1);
					
					pat_infoPattern = Pattern.compile("(&nbsp;)|(')|(<[\\s\\S]+?>)|(&#160;)");
					mat_infoMatcher = pat_infoPattern.matcher(detailString);
					info_report.report_description = mat_infoMatcher.replaceAll("");
					System.out.println(info_report.report_description);
				}
				pat_infoPattern = Pattern.compile("讲座因故取消");
				mat_infoMatcher = pat_infoPattern.matcher(singlePageString);
				if (mat_infoMatcher.find()) {
					info_report.report_iscanceled = 1;
					System.out.println("is cancelled!!!!!!!");
				}
				infoMap.put(info_report.report_link, info_report);
				
				//return;
			}
			//return;
		}
	}
	
	public void writeReoprtInfo2Mysql() {
		
		Connection connection;
	    Statement statement;
	    ResultSet res;
	    
		Info_report tmpInfo_report = new Info_report();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/lbs","liuyq", "haha");
		    if(connection != null)
		    {
		     System.out.println("连接成功！");
		    }
		    else
		    {
		    	System.out.println("连接失败！");
		    }
		    statement = connection.createStatement();  
		    
			for(Object o : infoMap.keySet()){ 
				tmpInfo_report.init();
			    tmpInfo_report = infoMap.get(o);
			   // System.out.println(tmpInfo_report.report_description);
			    
			    String sql_select = "select * from activity_report_profile where report_title = '" +
			    		tmpInfo_report.report_title +
			    		"';";
			    res = statement.executeQuery(sql_select);
			   
			    if(!res.next())
			    {
			    	System.out.println("No! Insert~");
			    	String sql_insert = "INSERT INTO  `lbs`.`activity_report_profile` (" +
			    		"`id` ," +
			    		"`report_title` ," +
			    		"`report_person` ," +
			    		"`report_place` ," +
			    		"`report_sponsor` ," +
			    		"`report_starttime` ," +
			    		"`report_description` ," +
			    		"`report_subject` ," +
			    		"`report_compere` ," +
			    		"`report_langurage` ," +
			    		"`report_person_introduction` ," +
			    		"`report_link` ," +
			    		"`report_state` ," +
			    		"`report_duration` ," +
			    		"`report_ticket` ," +
			    		"`report_iscanceled` ," +
			    		"`report_listener` ," +
			    		"`update_time`" +
			    		")" +
			    		"VALUES (" +
			    		"NULL ,  '" +
			    		tmpInfo_report.report_title+"',  '" +
			    		tmpInfo_report.report_person+"',  '" +
			    		tmpInfo_report.report_place+"',  '" +
			    		tmpInfo_report.report_sponsor+"',  '" +
			    		tmpInfo_report.report_starttime+"',  '" +
			    		tmpInfo_report.report_description+"',  '" +
			    		"tmpInfo_report.report_subject"+"',  '" +
			    		tmpInfo_report.report_compere+"',  '" +
			    		tmpInfo_report.report_langurage+"',  '" +
			    		tmpInfo_report.report_person_introduction+"',  '" +
			    		tmpInfo_report.report_link+"',  '" +
			    		tmpInfo_report.report_state+"',  '" +
			    		tmpInfo_report.report_duration+"',  '" +
			    		tmpInfo_report.report_ticket+"',  '" +
			    		tmpInfo_report.report_iscanceled+"',  '" +
			    		tmpInfo_report.report_listener+
			    		"',CURRENT_TIMESTAMP);";
			    
			    	System.out.println(sql_insert);
			    	statement.execute(sql_insert);
			    }
			    else 
			    {
			    	System.out.println("Yes! Update~");
			    	String sql_update = "UPDATE  `lbs`.`activity_report_profile` SET " +
			    			"`report_title`= '"+tmpInfo_report.report_title+
			    			"',`report_person`='" +tmpInfo_report.report_person+
				    		"',`report_place`='"+ tmpInfo_report.report_place+
				    		"',`report_sponsor`='"+ tmpInfo_report.report_sponsor+
				    		"',`report_starttime`='"+tmpInfo_report.report_starttime+
				    		"',`report_description`='"+tmpInfo_report.report_description+
				    		"',`report_subject`='"+"tmpInfo_report.report_subject"+
				    		"',`report_compere`= '" + tmpInfo_report.report_compere+
				    		"',`report_langurage`= '"+tmpInfo_report.report_langurage+
				    		"',`report_person_introduction`='"+ tmpInfo_report.report_person_introduction+
				    		"',`report_link`='"+tmpInfo_report.report_link+
				    		"',`report_state`='"+tmpInfo_report.report_state+
				    		"',`report_iscanceled`='"+tmpInfo_report.report_iscanceled+
				    		"',`report_listener`='"+tmpInfo_report.report_listener+
				    		"',`report_duration`='" + tmpInfo_report.report_duration+
				    		"',`report_ticket`='"+tmpInfo_report.report_ticket+
				    		"' WHERE `activity_report_profile`.`report_title`='"+
				    		tmpInfo_report.report_title+"';";
			    	System.out.println(sql_update);
			    	statement.executeUpdate(sql_update);
			    	
			    	}
			}
			statement.close();
		    connection.close();
		}
	    catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		ReportInfo reportInfo = new ReportInfo();
		//reportInfo.getReportInfo_kyybgxx();
		//
		reportInfo.getReportInfo_sem();
		reportInfo.writeReoprtInfo2Mysql();
	}

}
